package com.tenton.listener;

import com.tenton.pojo.Cart;
import com.tenton.service.CartService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * @Date: 2021/2/2
 * @Author: Tenton
 * @Description:
 */
@Component
@RabbitListener(queues = "my-dlx-queue")
public class CartListener {
    @Autowired
    CartService cartService;

    @RabbitHandler
    public void receive(HashMap<String, Cart> map) {
        //cart是rabbitMQ里面是购物车数据cart
        for (Cart cart : map.values()) {
            //根据用户Id查询对应的cart信息
            List<Cart> carlist = cartService.getCart(cart.getUserId());
            for (Cart cart1 : carlist) {
                //根据用户Id和cart修改时间是否与之前一致，
                //用户Id一致,购买数量不一致，则说明cart信息有修改，不删除cart信息
                //判断用户Id和购买数量是否一致
                if (cart.getUserId().equals(cart1.getUserId()) && cart.getNum().equals(cart1.getNum())) {
                    cartService.deleteCart(cart.getId());
                }
            }
        }
    }
}
